package com.cloud.util.page.ser.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.cloud.util.page.PageInfo;
import com.cloud.util.page.dao.IPageDao;
import com.cloud.util.page.ser.IPageService;

/**
 * 
 * 分页参数服务
 * 
 * @version 1.0
 * @date 2016-04-26
 * @author gaof
 */
@Service
public class PageService implements IPageService {

	@Resource(name="pageDao")
	private IPageDao pageDao;

	/**
	 * 获取分页结果信息
	 * 
	 * @param pageInfo
	 * @return
	 */
	@SuppressWarnings("rawtypes")
	public PageInfo getPageInfoList(PageInfo pageInfo) {

		/* 查询有多少条记录 */
		int totalCount = pageDao.getTotalCountByHbSql(pageInfo
				.getTotalCountHbSQL());
		pageInfo.setTotalCount(totalCount);
		if (totalCount < 0) {
			pageInfo.setResultList(new ArrayList());
			return pageInfo;
		}

		/* 设置查询页数 */
		int currentPage = pageInfo.getCurrentPage();
		if (currentPage > pageInfo.getTotalPageCount()) {
			pageInfo.setCurrentPage(1);
		}

		/* 查询记录 */
		List list = pageDao.getRangeInfoList(pageInfo.getListHbSql(), pageInfo
				.getCurrentPage(), pageInfo.getPageSize());
		pageInfo.setResultList(list);

		return pageInfo;

	}
	
	/**
	 * 获取分页结果信息(双联表查询)
	 * @param pageInfo
	 * @return
	 */
	public PageInfo getSQLPageInfo(PageInfo pageInfo){
		
		/* 查询有多少条记录 */
		int totalCount = pageDao.getTotalCountBySql(pageInfo
				.getTotalCountHbSQL());
		pageInfo.setTotalCount(totalCount);
		if (totalCount < 0) {
			pageInfo.setResultList(new ArrayList());
			return pageInfo;
		}

		/* 设置查询页数 */
		int currentPage = pageInfo.getCurrentPage();
		if (currentPage > pageInfo.getTotalPageCount()) {
			pageInfo.setCurrentPage(1);
		}

		/* 查询记录 */
		List list = pageDao.getSQLRangeInfoList(pageInfo.getListHbSql(), pageInfo
				.getCurrentPage(), pageInfo.getPageSize(), pageInfo.getEntityClass());
		pageInfo.setResultList(list);

		return pageInfo;
	}

}
